package com.ifreshwing.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Toast;

/**
 * Created by lz on 2017/4/27.
 */

public class ToastUtil {
    private static Toast toast;
    private static View view;

    @SuppressLint("ShowToast")
    private static void getToast(Context context) {
        if (toast == null) {
            toast = new Toast(context);
        }
        if (view == null) {
            view = Toast.makeText(context, "", Toast.LENGTH_SHORT).getView();
        }
        toast.setView(view);
    }

    public static void shortToast(Context context, String content) {
        // Toast.makeText(context, content, Toast.LENGTH_SHORT).show();
        showToast(context, content, Toast.LENGTH_SHORT);
    }

    public static void longToast(Context context, String content) {
        // Toast.makeText(context, content, Toast.LENGTH_LONG).show();
        showToast(context, content, Toast.LENGTH_LONG);
    }

    private static void showToast(Context context, CharSequence msg, int time) {
        try {
            getToast(context);
            toast.setText(msg);
            toast.setDuration(time);
            toast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * android L SnackBar
     * 必须基于CoordinatorLayout，否则将不会显示
     *
     * @param view      CoordinatorLayout
     * @param content   描述
     * @param listener  点击按钮事件
     * @param ActionTip 点击提示的文字
     * @author lz
     */
    public static void showSnackBar(View view, String content, NoDoubleClickListener listener, String ActionTip) {
        Snackbar.make(view, content, Snackbar.LENGTH_LONG).setAction(ActionTip, listener).show();
    }

    public static void showSnackBar(View view, String content) {
        Snackbar.make(view, content, Snackbar.LENGTH_LONG).show();
    }
}
